

cc.Class({
    extends: cc.Component,

    properties: {
        rankLabel: cc.Label,
        headSprite: cc.Sprite,
        nameLabel: cc.Label,
        scoreLabel: cc.Label,
        firstSpriteNode: cc.Node,
        bgSprite: cc.Sprite,
        //两种背景.
        bgSprites: {
            default: [],
            type: cc.SpriteFrame,
            visible: true
        },
    },

    // LIFE-CYCLE CALLBACKS:

    start () {

    },

    // update (dt) {},

    //i索引
    initView(data, i, self){
        this.bgSprite.node.active = !self;
        if(i == 0 && !self) this.firstSpriteNode.active = true;
        //背景交替.
        this.bgSprite.spriteFrame = this.bgSprites[i%2];
        //排名.
        this.rankLabel.string = self ? '我' : (i+1).toString();
        //头像.
        var avatarUrl = data.avatarUrl + "?aaa=aa.jpg";
        this.createImage(this.headSprite,avatarUrl);
        //昵称.
        this.nameLabel.string = data.nickname;
        //分数.
        var dList = data.KVDataList;
        for(var i = 0; i < dList.length; i++){
            //遍历键值对.
            if(dList[i].key == "RotateJump_MaxScore"){
                this.scoreLabel.string = dList[i].value;
                break;
            }
        }
    },

    createImage(sprite,url){
        let image = wx.createImage();
        image.onload = function () {
        let texture = new cc.Texture2D();
        texture.initWithElement(image);
        texture.handleLoadedTexture();
        sprite.spriteFrame = new cc.SpriteFrame(texture);
        sprite.node.width=100;
        sprite.node.height=100;
        };
        image.src = url;
    },
});
